package tech.yunland.shop.web;

import com.alibaba.fastjson.JSON;
import com.moyuer.bsf.common.Pagination;
import com.moyuer.bsf.common.PinYinUtil;
import com.moyuer.bsf.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tech.yunland.shop.domain.model.Tag;
import tech.yunland.shop.domain.model.TagGoods;
import tech.yunland.shop.domain.service.TagService;

import java.util.List;

/**
 * 商品标签控制器
 *
 * @author 王东阳<350456985@qq.com>
 */
@RestController
@RequestMapping("/tag")
public class TagController extends BaseController {

    @Autowired
    private TagService tagService;

    /**
     * 保存商品标签
     *
     * @param tag
     */
    @RequestMapping(value = "/", method = RequestMethod.POST)
    public void saveOrUpdate(@RequestBody Tag tag) {
        logger.info(JSON.toJSONString(tag, true));

        if (tag.getOrgan() == null) {
            tag.setOrgan(primaryOrgan);
        }

        List<Tag> existsTag = tagService.getList(tag.getName(), primaryOrganId);
        if (existsTag != null) {
            if (existsTag.size() > 1) {
                throw new RuntimeException("同名商品已存在");
            }
            else if (existsTag.size() == 1) {
                Tag t = existsTag.get(0);
                if (t.getName().equals(tag.getName())) {
                    throw new RuntimeException("同名商品已存在");
                }
            }
        }

        //分录对象赋值
        if (tag.getTagGoods() != null) {
            for (TagGoods tagGoods : tag.getTagGoods()) {
                tagGoods.setTag(tag);
            }
        }

        tag.setSpell(PinYinUtil.getFirstSpell(tag.getName()));

        tagService.saveOrUpdate(tag);

        saveLog("商品:[" + tag.getName() + "]保存成功！");
    }

    /**
     * 根据主键获取商品标签
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Tag get(@PathVariable String id) {
        return tagService.get(id);
    }

    /**
     * 根据主键清空商品标签
     *
     * @param id
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public void delete(@PathVariable String id) {
        Tag tag = tagService.get(id);
        tagService.del(id);
        saveLog("商品:[" + tag.getName() + "]标签删除成功！");
    }


    /**
     * 查询商品标签
     *
     * @param param
     * @param tagIds
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public Pagination<Tag> getResultInfos(
            @RequestParam(required = false) String param,
            @RequestParam(required = false) Integer page,
            @RequestParam(required = false) Integer rows) {


        Pagination<Tag> result = new Pagination<Tag>(page, rows);
        int starindex = result.getStartIdx();

        int count = tagService.getResultSize(param, organIds);
        List<Tag> records = tagService.getResultInfos(param, starindex, rows, organIds);

        result.setCount(count);
        result.setRecords(records);

        return result;
    }

    /**
     * 查询标签
     *
     * @param goodsId
     * @param tagId
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping(value = "/tag_goods/", method = RequestMethod.GET)
    public Pagination<TagGoods> getResultInfosTG(
            @RequestParam(required = false) String goodsId,
            @RequestParam(required = false) String tagId,
            @RequestParam(required = false) Integer page,
            @RequestParam(required = false) Integer rows) {

        Pagination<TagGoods> result = new Pagination<TagGoods>(page, rows);
        int startIdx = result.getStartIdx();

        int count = tagService.getResultSizeTG(goodsId, tagId);
        List<TagGoods> records = tagService.getResultInfoTG(goodsId, tagId, startIdx, rows);

        result.setCount(count);
        result.setRecords(records);

        return result;
    }
}
